// Copyright (c) Microsoft Corporation.  All rights reserved.

using System.Windows.Forms;
using Microsoft.WindowsAPICodePack.DirectX.Direct2D1;
using System;
using System.IO;


namespace D2DPaint
{
    enum BrushType
    {
        None,
        Solid,
        Bitmap,
        LinearGradiant,
        RadialGradient
    }

    public partial class BrushDialog : Form
    {
        internal ColorF color = new ColorF(0,0,0,1);

        internal ColorF gradColor1 = new ColorF(0, 0, 0, 1);
        internal ColorF gradColor2 = new ColorF(1, 1, 1, 1);
        
        internal float opacity = 1.0f;
        Pain2DForm parent;
        string imageFilename = null;

        public BrushDialog(Pain2DForm parent)
        {
            this.parent = parent;
            InitializeComponent();
            FillBrushesListBox();
        }

        private void FillBrushesListBox()
        {
            this.brushesList.Items.Clear();
            foreach (Brush brush in parent.brushes)
            {
                if (brush is SolidColorBrush)
                {
                    SolidColorBrush solidBrush = brush as SolidColorBrush;
                    this.brushesList.Items.Add(
                        string.Format("Solid: R={0}, G={1}, B={2}, A={3}, Opacity={4}", solidBrush.Color.R, solidBrush.Color.G, solidBrush.Color.B, solidBrush.Color.A, solidBrush.Opacity));
                }
                else if (brush is BitmapBrush)
                {
                    BitmapBrush bitmapBrush = brush as BitmapBrush;
                    this.brushesList.Items.Add(
                        string.Format("Bitmap Brush: Extended Mode X={0}, Extended Mode Y={1}, Inter. Mode={2}", bitmapBrush.ExtendModeX, bitmapBrush.ExtendModeY, bitmapBrush.InterpolationMode));
                }
                else
                {
                    this.brushesList.Items.Add(brush);
                }
            }
            brushesList.SelectedIndex = parent.currentBrushIndex;
            
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            SelectColor();
        }

        private void SelectColor()
        {
            colorDialog1.Color = System.Drawing.Color.Black;
            if (colorDialog1.ShowDialog() != DialogResult.Cancel)
            {
                color = new ColorF(
                    colorDialog1.Color.R / 255F,
                    colorDialog1.Color.G / 255F,
                    colorDialog1.Color.B / 255F,
                    colorDialog1.Color.A / 255F);

                colorLabel.Text = string.Format("R = {0}, G = {1}, B = {2}, A = {3}", color.R, color.G, color.B, color.A);
            }
        }

        private void addBrushButton_Click(object sender, System.EventArgs e)
        {
            parent.brushes.Add(
                        parent.renderTarget.CreateSolidColorBrush(
                        color,
                        new BrushProperties(opacity, new Matrix3x2F(1, 0, 0, 1, 0, 0))));

            parent.currentBrushIndex = parent.brushes.Count - 1;

            FillBrushesListBox();
        }

        private void transparencyValues_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            float f;
            if (float.TryParse(transparencyValues.Text as string, out f))
            {
                this.opacity = f;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            parent.currentBrushIndex = brushesList.SelectedIndex;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.DefaultExt = "*.jpg;*.png";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.imageFilename = dialog.FileName;
                imageFileLabel.Text = Path.GetFileName(imageFilename);
                
            }
        }

        private void extendedModeXComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void extendedModeYComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void addBitmapBrushBotton_Click(object sender, EventArgs e)
        {
            ExtendMode ex = extendedModeXComboBox.SelectedIndex > 0 ? (ExtendMode)extendedModeXComboBox.SelectedIndex :ExtendMode.Clamp;
            ExtendMode ey = extendedModeYComboBox.SelectedIndex > 0 ? (ExtendMode)extendedModeYComboBox.SelectedIndex : ExtendMode.Clamp;

            D2DBitmap brushBitmap = BitmapUtilities.LoadBitmapFromFile(parent.renderTarget, parent.wicFactory, imageFilename);
            BitmapBrush brush = parent.renderTarget.CreateBitmapBrush(
                brushBitmap, 
                new BitmapBrushProperties(
                    ex, ey, 
                    BitmapInterpolationMode.NearestNeighbor), 
                    new BrushProperties(
                        opacity, 
                        new Matrix3x2F(1, 0, 0, 1, 0, 0)));
            parent.brushes.Add(brush);
            parent.currentBrushIndex = parent.brushes.Count - 1;
            FillBrushesListBox();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            float f;
            if (float.TryParse(comboBox2.Text as string, out f))
            {
                this.opacity = f;
            }
        }

        private void gradiantBrushColor1button_Click(object sender, EventArgs e)
        {

            colorDialog1.Color = System.Drawing.Color.Black;
            if (colorDialog1.ShowDialog() != DialogResult.Cancel)
            {
                gradColor1 = new ColorF(
                    colorDialog1.Color.R / 255F,
                    colorDialog1.Color.G / 255F,
                    colorDialog1.Color.B / 255F,
                    colorDialog1.Color.A / 255F);

                gradBrushColor1Label.Text = string.Format("R = {0}, G = {1}, B = {2}, A = {3}", gradColor1.R, gradColor1.G, gradColor1.B, gradColor1.A);
            }
        }

        private void gradiantBrushColor2Button_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = System.Drawing.Color.Black;
            if (colorDialog1.ShowDialog() != DialogResult.Cancel)
            {
                gradColor2 = new ColorF(
                    colorDialog1.Color.R / 255F,
                    colorDialog1.Color.G / 255F,
                    colorDialog1.Color.B / 255F,
                    colorDialog1.Color.A / 255F);

                gradBrushColor2Label.Text = string.Format("R = {0}, G = {1}, B = {2}, A = {3}", gradColor2.R, gradColor2.G, gradColor2.B, gradColor2.A);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ExtendMode ex = gradBrushExtendModeCombo.SelectedIndex > 0 ? (ExtendMode)gradBrushExtendModeCombo.SelectedIndex : ExtendMode.Clamp;
            Gamma gamma = gammaComboBox.SelectedIndex > 0 ? (Gamma)gammaComboBox.SelectedIndex : Gamma.Gamma_10;

            GradientStop[] stops = 
            {
                new GradientStop(0, gradColor1),
                new GradientStop(1.0f, gradColor2),
            };

            GradientStopCollection stopCollection = parent.renderTarget.CreateGradientStopCollection(
                stops, gamma, ex);

            LinearGradientBrush brush = parent.renderTarget.CreateLinearGradientBrush(
                new LinearGradientBrushProperties(
                    new Point2F(0,0), new Point2F(15,15)),
                    stopCollection);

            parent.brushes.Add(brush);
            parent.currentBrushIndex = parent.brushes.Count - 1;
            FillBrushesListBox();

        }

        private void button4_Click(object sender, EventArgs e)
        {
            ExtendMode ex = radialExtendCombo.SelectedIndex > 0 ? (ExtendMode)radialExtendCombo.SelectedIndex : ExtendMode.Clamp;
            Gamma gamma = radialGammaCombo.SelectedIndex > 0 ? (Gamma)radialGammaCombo.SelectedIndex : Gamma.Gamma_10;

            GradientStop[] stops = 
            {
                new GradientStop(0, gradColor1),
                new GradientStop(1.0f, gradColor2),
            };

            GradientStopCollection stopCollection = parent.renderTarget.CreateGradientStopCollection(
                stops, gamma, ex);

            RadialGradientBrush brush = parent.renderTarget.CreateRadialGradientBrush(
                new RadialGradientBrushProperties(
                    new Point2F(50, 50), new Point2F(0, 0), 50, 50),
                    stopCollection);

            parent.brushes.Add(brush);
            parent.currentBrushIndex = parent.brushes.Count - 1;
            FillBrushesListBox();
        }

        private void SelectRadialColor1_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = System.Drawing.Color.Black;
            if (colorDialog1.ShowDialog() != DialogResult.Cancel)
            {
                gradColor1 = new ColorF(
                    colorDialog1.Color.R / 255F,
                    colorDialog1.Color.G / 255F,
                    colorDialog1.Color.B / 255F,
                    colorDialog1.Color.A / 255F);

                radialBrushColor1Label.Text = string.Format("R = {0}, G = {1}, B = {2}, A = {3}", gradColor1.R, gradColor1.G, gradColor1.B, gradColor1.A);
            }
        }

        private void SelectRadialColor2_Click(object sender, EventArgs e)
        {
            colorDialog1.Color = System.Drawing.Color.Black;
            if (colorDialog1.ShowDialog() != DialogResult.Cancel)
            {
                gradColor2 = new ColorF(
                    colorDialog1.Color.R / 255F,
                    colorDialog1.Color.G / 255F,
                    colorDialog1.Color.B / 255F,
                    colorDialog1.Color.A / 255F);

                radialBrushColor2Label.Text = string.Format("R = {0}, G = {1}, B = {2}, A = {3}", gradColor2.R, gradColor2.G, gradColor2.B, gradColor2.A);
            }
        }


    }
}
